package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.RoleDto;
import com.itheima.dto.RoleMenuDto;
import com.itheima.entry.PageResult;
import com.itheima.entry.QueryPageBean;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.*;
import com.itheima.service.RoleMenuService;
import com.itheima.service.RoleService;
import com.itheima.service.RoleUserService;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;

import java.util.List;

/**
 * @author jshstart
 * @creat 2020-11-23 9:43
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Reference
    private RoleUserService roleUserService;
    @Reference
    private RoleMenuService roleMenuService;
    @Override
    public PageResult findAllRoleByPage(QueryPageBean queryPageBean) {
        Page<Role> page=null;
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString()!=null&&"".equals(queryPageBean.getQueryString())==false){
            queryWrapper.and(wrapper ->{
                wrapper.like("role_name",queryPageBean.getQueryString()).or()
                        .like("role_code",queryPageBean.getQueryString());
            });
        }
        page =page(new Page<Role>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return pageResult;
    }

    @Override
    public boolean addRole(RoleDto roleDto) {
        this.saveOrUpdate(roleDto.getRole());
        Long roleId = roleDto.getRole().getId();
        Long[] userIds = roleDto.getUserIds();
        roleUserService.deleteById(roleId);
        if (userIds!=null&&roleId!=null){
            for (Long userId : userIds) {
                RoleUser roleUser = new RoleUser();
                roleUser.setRoleId(roleId);
                roleUser.setUserId(userId);
                roleUserService.saveOrUpdate(roleUser);
            }
        }
        return true;
    }

    @Override
    public Long[] findUserByRoleId(Long id) {
     return roleUserService.findUserByRoleId(id);
    }

    @Override
    public boolean distribution(RoleDto roleDto) {
        Long roleId = roleDto.getRole().getId();
        Long[] userIds = roleDto.getUserIds();
        roleUserService.deleteById(roleId);
        if (userIds!=null&&roleId!=null){
            for (Long userId : userIds) {
                RoleUser roleUser = new RoleUser();
                roleUser.setRoleId(roleId);
                roleUser.setUserId(userId);
                roleUserService.saveOrUpdate(roleUser);
            }
        }
        return true;
    }

    @Override
    public void deleteById(Long id) {

        Role role = baseMapper.selectById(id);
        if (role!=null){
            role.setIs_delete(1);
            this.updateById(role);
        }
    }

    @Override
    public Long[] findMenuByRoleId(Long id) {
        return roleMenuService.findMenuByRoleId(id);
    }

    @Override
    public boolean addMenu(RoleMenuDto roleMenuDto) {
        Long roleId = roleMenuDto.getRole().getId();
        Long[] menuIds = roleMenuDto.getMenuIds();
        roleMenuService.deleteById(roleId);
        if (menuIds!=null&&roleId!=null){
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenuService.saveOrUpdate(roleMenu);
            }

        }
        return true;
    }

    @Override
    public List<Role> findAllRole() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<Role> list = this.list(queryWrapper);
        return list;
    }
}
